package ProductGUIs;

import Model.Product;
import Model.InternalDBManager;
import javax.persistence.*;
import javax.swing.ImageIcon;
import Supermarket.AppMessages;
import javax.swing.JFrame;

/**
 *
 * @author bagi
 */
public class EditProductForm extends javax.swing.JFrame {

    private static EntityManager em;
    Product product = new Product();
    String FloatNumberErrorMsg="Μόνο θετικοί δεκαδικοί αριθμοί: ως υποδιαστολή η (.)";
    String IntNumberErrorMsg="Μόνο θετικοί φυσικοί αριθμοί.";
    JFrame thisFrame;
    //μεταβλητή καθορισμού τροποποίησης (false) ή δημιουργίας νέου προϊόντος (true)
    boolean statusLabel;
    boolean statusUpdate=false;
    
    public EditProductForm() {
        em = InternalDBManager.getEm();
        initComponents();
        this.setIconImage(new
                ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage()); 
        setAlwaysOnTop(true);
    }
    
    public EditProductForm(Product p, boolean status) {
        em = InternalDBManager.getEm();
        statusLabel = status;
        this.product = p;
        initComponents();
        this.setIconImage(new
                ImageIcon(getClass().getResource("..\\Images\\cart.png")).getImage()); 
        setAlwaysOnTop(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        productInDB = product;
        ProductForm = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ProductNameField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        ProductCodeField = new javax.swing.JTextField();
        ErrorMessageLabel = new javax.swing.JLabel();
        ProductNameMissingLabel = new javax.swing.JLabel();
        ProductCodeMissingLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ProductPriceField = new javax.swing.JTextField();
        ProductPointsField = new javax.swing.JTextField();
        ProductPointsMissingLabel = new javax.swing.JLabel();
        ProductPriceMissingLabel = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        RegisterButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Διαχείριση Στοιχείων Προϊόντος");
        setResizable(false);

        ProductForm.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        ProductForm.setNextFocusableComponent(ProductNameField);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Περιγραφή:");

        ProductNameField.setColumns(30);
        ProductNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ProductNameField.setToolTipText("");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productInDB, org.jdesktop.beansbinding.ELProperty.create("${productName}"), ProductNameField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        ProductNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ProductNameFieldActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Κωδ. προϊόντος:");

        ProductCodeField.setColumns(50);
        ProductCodeField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productInDB, org.jdesktop.beansbinding.ELProperty.create("${code}"), ProductCodeField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        ErrorMessageLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ErrorMessageLabel.setForeground(new java.awt.Color(255, 0, 0));
        ErrorMessageLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        ErrorMessageLabel.setToolTipText("");

        ProductNameMissingLabel.setForeground(java.awt.Color.red);
        ProductNameMissingLabel.setText("    ");
        ProductNameMissingLabel.setToolTipText("");

        ProductCodeMissingLabel.setForeground(java.awt.Color.red);
        ProductCodeMissingLabel.setText("    ");
        ProductCodeMissingLabel.setToolTipText("");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Πόντοι αγοράς:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Τιμή τεμαχίου €:");

        ProductPriceField.setColumns(50);
        ProductPriceField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ProductPriceField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ProductPriceField.setToolTipText("Καταχωρήστε αριθμό. Ως υποδιαστολή χρησιμοποιήστε την (.)");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productInDB, org.jdesktop.beansbinding.ELProperty.create("${price}"), ProductPriceField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        ProductPriceField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                ProductPriceFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                ProductPriceFieldFocusLost(evt);
            }
        });

        ProductPointsField.setColumns(50);
        ProductPointsField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        ProductPointsField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ProductPointsField.setToolTipText("Φυσικός αριθμός ίσος ή μεγαλύτερος του 0");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, productInDB, org.jdesktop.beansbinding.ELProperty.create("${points}"), ProductPointsField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        ProductPointsField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                ProductPointsFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                ProductPointsFieldFocusLost(evt);
            }
        });

        ProductPointsMissingLabel.setForeground(java.awt.Color.red);
        ProductPointsMissingLabel.setText("    ");
        ProductPointsMissingLabel.setToolTipText("");

        ProductPriceMissingLabel.setForeground(java.awt.Color.red);
        ProductPriceMissingLabel.setText("    ");
        ProductPriceMissingLabel.setToolTipText("");

        javax.swing.GroupLayout ProductFormLayout = new javax.swing.GroupLayout(ProductForm);
        ProductForm.setLayout(ProductFormLayout);
        ProductFormLayout.setHorizontalGroup(
            ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ErrorMessageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ProductFormLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ProductCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ProductPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(1, 1, 1)
                        .addComponent(ProductPriceMissingLabel)))
                .addGap(6, 6, 6)
                .addGroup(ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addComponent(ProductCodeMissingLabel)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addComponent(ProductNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ProductNameMissingLabel))
                    .addGroup(ProductFormLayout.createSequentialGroup()
                        .addComponent(ProductPointsField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ProductPointsMissingLabel))))
        );
        ProductFormLayout.setVerticalGroup(
            ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ProductFormLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ProductCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ProductCodeMissingLabel)
                    .addComponent(ProductNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ProductNameMissingLabel)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(ProductFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ProductPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ProductPriceMissingLabel)
                    .addComponent(jLabel4)
                    .addComponent(ProductPointsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ProductPointsMissingLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ErrorMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/supermarket_320.png"))); // NOI18N

        RegisterButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        RegisterButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/save_16.png"))); // NOI18N
        RegisterButton.setText("Καταχώρηση");
        RegisterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegisterButtonActionPerformed(evt);
            }
        });

        CancelButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        CancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/cancel_16.png"))); // NOI18N
        CancelButton.setText("Ακύρωση");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ProductForm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(RegisterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ProductForm, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(RegisterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel10))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ProductNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ProductNameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ProductNameFieldActionPerformed

    private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegisterButtonActionPerformed
        //Σβήνουμε τυχόν μηνύματα λάθους (labels)
        initLabels();
        //αν η μεταφορά και αποθήκευση έγινε κανονικά κλείσιμο της φόρμας
        statusUpdate=DataManager();
        if(statusUpdate){
            this.dispose();
        }
    }//GEN-LAST:event_RegisterButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed

        this.dispose();
    }//GEN-LAST:event_CancelButtonActionPerformed

    //Καθαρισμός μηνύματος μετά την απομάκρυνση από το πεδίο της τιμής του προϊόντος
    private void ProductPriceFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ProductPriceFieldFocusLost
        NumbersOnly (null,null,null);
    }//GEN-LAST:event_ProductPriceFieldFocusLost
    
    //Εμφάνιση μηνύματος σωστής μορφής αριθμού στο πεδίο της τιμής του προϊόντος
    private void ProductPriceFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ProductPriceFieldFocusGained
        NumbersOnly ("(*)",null,FloatNumberErrorMsg);
    }//GEN-LAST:event_ProductPriceFieldFocusGained
    
    //Εμφάνιση μηνύματος σωστής μορφής αριθμού στο πεδίο των πόντων
    private void ProductPointsFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ProductPointsFieldFocusGained
        NumbersOnly (null,"(*)",IntNumberErrorMsg);
    }//GEN-LAST:event_ProductPointsFieldFocusGained

    //Καθαρισμός μηνύματος μετά την απομάκρυνση από το πεδίο των πόντων
    private void ProductPointsFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ProductPointsFieldFocusLost
        NumbersOnly (null,null,null);
    }//GEN-LAST:event_ProductPointsFieldFocusLost
    
    //μέθοδος που εμφανίζει μήνυμα για τη σωστή μορφή των αριθμών κατά την καταχώρηση
    public void NumbersOnly(String msg1, String msg2, String msg3){
        ProductPriceMissingLabel.setText(msg1);
        ProductPointsMissingLabel.setText(msg2);
        ErrorMessageLabel.setText(msg3);
    }
    
    //μέθοδος που αναλαμβάνει να αποθηκεύσει τα δεδομένα της φόρμας
    public boolean DataManager(){
        boolean success = false;
        boolean ProductDataInCorrectForm = registerProduct();
        this.setAlwaysOnTop(false);
        //αν τα δεδομένα της φόρμας είναι στη σωστή μορφή 
        if (ProductDataInCorrectForm){
            //αν πρόκειται για εισαγωγή νέου προϊόντος
            if(statusLabel){
                //και το προϊόν δεν υπάρχει στη ΒΔ
                if (!isProductInDB()){
                    if (UpdateProductToDB())
                        success = true;
                //αν το προϊόν υπάρχει στη ΒΔ
                }else{
                    AppMessages.DataExistsMessage(this);   
                    this.setAlwaysOnTop(true);
                } 
            } else{ 
                //αν πρόκειται για τροποποίηση στοιχείων προϊόντος (statusLabel=false)
                //ζητείται επιβεβαίωση των αλλαγών πριν την ενημέρωση της ΒΔ (statusLabel=false)
                int confirmChoice = AppMessages.ConfirmationMessage(this);
                this.setAlwaysOnTop(true);
                if (confirmChoice==0){
                    if (UpdateProductToDB())
                        success = true;
                }
            }
        }
        return success;
    }
    
    //έλεγχος και μεταφορά των πεδίων της φόρμας σε αντικείμενο product
    //για την καταχώρηση στη βάση
    public boolean registerProduct(){
        boolean success = true;
        //Έλεγχος εισαγωγής ονόματος προϊόντος
        if(ProductNameField.getText().isEmpty()){
            ErrorMessageLabel.setText(AppMessages.FieldsMissingMsg);
            ProductNameMissingLabel.setText("(*)");
            success = false;
        }else{
            product.setProductName(ProductNameField.getText());   
        }
        //Έλεγχος εισαγωγής κωδικού προϊόντος
        if(ProductCodeField.getText().isEmpty()){
           ErrorMessageLabel.setText(AppMessages.FieldsMissingMsg);
           ProductCodeMissingLabel.setText("(*)");
           success = false;
        }else{
            product.setCode(ProductCodeField.getText());   
        }
        //Έλεγχος εισαγωγής πόντων αγοράς
        if(ProductPointsField.getText().isEmpty()){
           ErrorMessageLabel.setText(AppMessages.FieldsMissingMsg);
           ProductPointsMissingLabel.setText("(*)");
           success = false;
        }else{
            //Εάν η τιμή του κελιού δεν είναι φυσικός αριθμός, αναιρεί την καταχώρηση
            if (!AppMessages.NumberEntryOnly(this, ProductPointsField.getText())){
            ProductPointsField.setText("");
            } else{
            product.setPoints(Integer.valueOf(ProductPointsField.getText())); 
            }
        }
        //Έλεγχος εισαγωγής τιμής τεμαχίου
        if(ProductPriceField.getText().isEmpty()){
           ErrorMessageLabel.setText(AppMessages.FieldsMissingMsg);
           ProductPriceMissingLabel.setText("(*)");
           success = false;
        }else{
            //Εάν η τιμή του κελιού δεν είναι δεκαδικός αριθμός, αναιρεί την καταχώρηση
            if (!AppMessages.NumberEntryOnly(this, ProductPriceField.getText())){
                ProductPriceField.setText("");
                success = false;
            } else{
                product.setPrice(Float.valueOf(ProductPriceField.getText()));   
            }
        }
        //επίστρεψε το αποτέλεσμα της μεταφοράς δεδομένων
        return success;
    }
 
    //Πρόσθήκη-Τροποποίηση στοιχείων προϊόντος στη ΒΔ
    private boolean UpdateProductToDB() {
        this.setAlwaysOnTop(false);
        try {
             if (!em.getTransaction().isActive()){
                em.getTransaction().begin();
            }
            if (statusLabel)
               em.persist(product);   //εάν νέο προϊόν
            else em.merge(product);   //εάν τροποποίηση υφιστάμενου
            em.getTransaction().commit();
            //Εμφανίζει μήνυμα επιτυχίας 
            AppMessages.SuccessMessage(this);
            this.setAlwaysOnTop(true);
            return true;
        } catch (SecurityException e) {
            AppMessages.FailureMessage(this, e);
            em.getTransaction().rollback();
            this.setAlwaysOnTop(true);
            return false;
        }
    }
    
    //μέθοδος που αρχικοποιεί όλα τα labels που χρησιμοποιούνται
    //για την εμφάνιση μηνυμάτων
    private void initLabels() {
        ErrorMessageLabel.setText("");
        ProductNameMissingLabel.setText("");
        ProductCodeMissingLabel.setText("");
        ProductPointsMissingLabel.setText("");
        ProductPriceMissingLabel.setText("");
    }

    //boolean μέθοδος που ελέγχει αν το προϊόν υπάρχει στη ΒΔ
    private boolean isProductInDB(){
        boolean success = false;
        try{
            if (!em.getTransaction().isActive()){
                    em.getTransaction().begin();
            }  
            //ψάχνει το προϊόν με το όνομά του 
            Product p_exists = (Product)em.createNamedQuery("Product.findByProductName")
                .setParameter("productName", product.getProductName()).getSingleResult();
            //αν βρεθεί επιστρέφει true
            success = true;
        }catch(NoResultException e){
            //αν δε βρεθεί επιστρέφει false
            success = false;
        }
        return success;
    } 
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditProductForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditProductForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditProductForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditProductForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditProductForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel ErrorMessageLabel;
    private javax.swing.JTextField ProductCodeField;
    private javax.swing.JLabel ProductCodeMissingLabel;
    private javax.swing.JPanel ProductForm;
    private javax.swing.JTextField ProductNameField;
    private javax.swing.JLabel ProductNameMissingLabel;
    private javax.swing.JTextField ProductPointsField;
    private javax.swing.JLabel ProductPointsMissingLabel;
    private javax.swing.JTextField ProductPriceField;
    private javax.swing.JLabel ProductPriceMissingLabel;
    private javax.swing.JButton RegisterButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private Model.Product productInDB;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
